home *** CD-ROM | disk | FTP | other *** search
- Path: druid.borland.com!usenet
- From: pete@borland.com (Pete Becker)
- Newsgroups: comp.lang.c++
- Subject: Re: Q: Pointer to member fctn within member fctn
- Date: 16 Feb 1996 19:43:15 GMT
- Organization: Borland International
- Message-ID: <4g2moj$f81@druid.borland.com>
- References: <4fgab1$h27@newsserv.zdv.uni-tuebingen.de>
- NNTP-Posting-Host: pbecker.borland.com
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=ISO-8859-1
- X-Newsreader: WinVN 0.99.5
-
- In article <4fgab1$h27@newsserv.zdv.uni-tuebingen.de>,
- hans.loeffler@student.uni-tuebingen.de says...
- >
- >I have a problem calling a member function through a pointer within
- >another member function
- >Consider:
- >
- >typedef void (*pmfctn)(int); //pointer to member function
- >
-
- Unfortunately, the compiler didn't read your comment, so it didn't know that
- you meant pmfctn to be a pointer to member function. It read the actual
- declaration, and figured that pmfctn is a pointer to an ordinary function that
- takes an int and returns void. If you change the typedef to actually define a
- pointer to member function it will work much better.
-
- class X;
- typedef void (X::*pmfctn)(int); // pointer to member function of X.
-
- >class X {
- >void f1(int);
- >void f2(int);
- >
- >pmfctn pf1, pf2;
- >
- >void f3(pmfctn);
- >};
- >
- >void X::f1(void) {
- > // do something
- > ...
- > return;
- >}
- >...
- >
- >void X::f3(pmfctn pf) {
- >
- >int a = 5;
- >// I want to call f1 or f2 now
- >// problem here:
- >this->*pf(a); // **
- >}
-
- This should not compile, even with the correct definition of pmfctn. It needs
- an additional set of parentheses:
-
- (this->*pf)(a);
-
- As originally written, it says to call pf(a) and use the result as the
- right-hand operand of ->*.
-
- > // ** this results in a runtime error with Borland C++ V4.5, when
- >calling f3(pf1) or f3(pf2)
- >// I initialize pf1 as:
- >pf1 = &X::f1;
- >
- >In books I only saw things like
- >
- >X *px;
- >
- >px->*pf1(a);
- >which call the functions from an object and not within a member
- >function
- >
- >What is wrong with my solution?
- >
- >Hans
- >
-
-